package fun.easycode.jointblock.core;

import fun.easycode.jointblock.exception.CheckException;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * UserHolder
 *  用户持有者，用来操作当前用户信息
 * @author xuzhe
 */
public abstract class UserHolder implements ApplicationContextAware {

    private static ApplicationContext context;

    /**
     * 获取UserHolder实例
     * @return UserHolder
     */
    public static UserHolder getInstance(){
        return context.getBean(UserHolder.class);
    }

    /**
     * 获取当前用户id
     * @return String
     */
    public abstract String getUserId();

    /**
     * 获取当前登录用户
     * @return 登录用户
     * @param <T> 登录用户泛型
     */
    public <T extends UserInfo> T getUser(){
        throw new CheckException("先重写getUser方法!");
    }

    /**
     * 判断是否登录
     * @return 是否
     */
    public boolean isLogin(){
        throw new CheckException("先重写isLogin方法！");
    }

    /**
     *  根据用户名强制下线用户
     * @param username 用户名
     */
    public void offline(String username){
        throw new CheckException("先重写offline方法！");
    }


    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 子类可以获取context
     * @return ApplicationContext
     */
    protected static ApplicationContext getContext(){
        return context;
    }
}
